package org.gridlab.gridsphere.portlets.visualization;

import org.gridlab.gridsphere.services.visualization.*;
import java.io.IOException;
import org.gridlab.gridsphere.provider.portletui.beans.FileInputBean;
import org.gridlab.gridsphere.provider.portletui.beans.CheckBoxBean;
import org.gridlab.gridsphere.provider.portletui.beans.RadioButtonBean;
import org.gridlab.gridsphere.provider.portletui.beans.TextBean;
import org.gridlab.gridsphere.provider.event.jsr.RenderFormEvent;
import org.gridlab.gridsphere.provider.event.jsr.ActionFormEvent;
import org.gridlab.gridsphere.event.ActionEvent;
import org.gridlab.gridsphere.provider.portlet.jsr.ActionPortlet;
import javax.servlet.UnavailableException;
import javax.portlet.*;

/**
 * Portlet demonstracyjny
 * @author Pawel Koperek, Krzysztof Goj
 */
public class ExamplePortlet extends ActionPortlet {
      private static final String DISPLAY_PAGE = "gsvis.jsp";
      private static final String CHART_PAGE = "view_chart.jsp";

      private XMLDataProvider fileDataProvider;
      private ChartGenerator pieChartGenerator;
      private ChartGenerator barChartGenarator;
      private ChartGenerator lineChartGenerator;
      private ChartGenerator areaChartGenerator;
      private ChartGenerator scp2DChartGenerator;
      private ChartGenerator scp3DChartGenerator;
      private ChartGenerator debugChartGenerator;
      private ChartGenerator[] chartGenerators;

    /**
     * Inicjalizuje portlet
     */
      public void init(PortletConfig config) throws PortletException {
          super.init(config);

          fileDataProvider = new XMLDataProvider();
          pieChartGenerator = new PieChartGenerator(fileDataProvider, getPortletContext());
          barChartGenarator = new BarChartGenerator(fileDataProvider, getPortletContext());
          areaChartGenerator = new AreaChartGenerator(fileDataProvider, getPortletContext());
          lineChartGenerator = new LineChartGenerator(fileDataProvider, getPortletContext());
          scp2DChartGenerator = new Scatterplot2DChartGenerator(fileDataProvider, getPortletContext());
          scp3DChartGenerator = new Scatterplot3DChartGenerator(fileDataProvider, getPortletContext());
          debugChartGenerator = new DebugChartGenerator(fileDataProvider, getPortletContext());
          ChartGenerator[] tmp = {
              barChartGenarator,
              pieChartGenerator,
              areaChartGenerator,
              lineChartGenerator,
              scp2DChartGenerator,
              scp3DChartGenerator,
              debugChartGenerator
          };
          chartGenerators = tmp;

          DEFAULT_VIEW_PAGE = "prepare";
      }

    /**
     * Powrót ze strony wykresu do strony wprowadzania danych
     */
    public void backToViewPage(ActionFormEvent event) throws PortletException {
       	setNextState(event.getActionRequest(), DEFAULT_VIEW_PAGE);
    }

    /**
     * Metoda wykonywana kiedy użytkownik zgłosi plik do załadowania
     */
    public void upload(ActionFormEvent event) throws PortletException {
	FileInputBean fileUploadBean = null;

	try {
		fileUploadBean = event.getFileInputBean("fileUploadBean");
	} catch(IOException e) {
		e.printStackTrace();
	}

        if(fileUploadBean == null) event.getActionResponse().setRenderParameter("fileName", "I/O Error!" );
        else {
            event.getActionResponse().setRenderParameter("fileName", fileUploadBean.getFileName());
            try {
                if(fileUploadBean.getFileName() != null && !fileUploadBean.getFileName().equals("")) {
                    fileDataProvider.clear();
                    fileDataProvider.parse(fileUploadBean.getInputStream());
                }
            } catch(Exception e) {
                    throw new PortletException(e);
            }
        }

       	setNextState(event.getActionRequest(), DEFAULT_VIEW_PAGE);
    }

    /**
     * Przygotowuje stronę do wyświetlenia
     */
    public void prepare(RenderFormEvent event) throws PortletException {  
	String name = event.getRenderRequest().getParameter("fileName");
          TextBean uploadedFile = event.getTextBean("uploadedFile");
          if (name == null || name.equals("")) {
               uploadedFile.setValue("<b>NO FILE !!!</b>");
          } else {
               uploadedFile.setValue(name);
          }

          setNextState(event.getRenderRequest(), DISPLAY_PAGE);
    }

    /**
     * Generuje wykres
     */
    public void generateChart(ActionFormEvent event) throws PortletException {
        ActionRequest request = event.getActionRequest();

        request.getPortletSession().setAttribute("chartGenerators", chartGenerators);
       	setNextState(request, CHART_PAGE);	
    }

}

